package io.itit.ecp.admin.provide.helper;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Pair;
import io.itit.ecp.admin.server.service.ICarbonEmissionFactorService;
import io.itit.ecp.admin.server.service.ICarbonSinkFactorService;
import io.itit.ecp.admin.server.service.IEmissionReductionFactorService;
import io.itit.grass.common.server.helper.RedisHelper;
import io.itit.grass.common.utils.Y666Utils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.Objects;
import java.util.stream.Collectors;

/*
 **********************
 * @Author tangdongming
 * @ClassName CarbonAbsFatorHelper
 * @Description
 * @Date 2023/12/6 18:19
 **********************
 **/
public abstract class CarbonAbsFactorHelper implements InitializingBean {

    // 2个月
    protected static long expire = 2 * 30 * 24 * 60 * 60;

    @Resource
    protected RedisHelper redisHelper;

    @Resource
    protected ICarbonEmissionFactorService carbonEmissionFactorService;
    @Resource
    protected ICarbonSinkFactorService carbonSinkFactorService;
    @Resource
    protected IEmissionReductionFactorService emissionReductionFactorService;


    /**
     * 计算天碳减量
     *
     * @param type
     * @param date
     * @param area
     * @return
     */
    public Pair<BigDecimal, String> calcDay(String type, Date date, BigDecimal area) {
        // 获取面积年固碳量缓存
        BigDecimal factor = getCacheFactor(type, date);
        return calcDay(factor, area);
    }

    public Pair<BigDecimal, String> calcDay(BigDecimal factor, BigDecimal area) {
        BigDecimal ra = new BigDecimal(365);
        BigDecimal multiply = Y666Utils.multiply(area, factor);
        BigDecimal divide = Y666Utils.divide(multiply, ra, 8);
        return Pair.of(divide, area + "*" + factor + "/" + ra);
    }

    public Pair<BigDecimal, String> calc(String type, Date date, BigDecimal meterValue) {
        BigDecimal factor = getCacheFactor(type, date);
        return calc(meterValue, factor);
    }

    public Pair<BigDecimal, String> calc(BigDecimal meterValue, BigDecimal factor) {
        BigDecimal multiply = Y666Utils.multiply(meterValue, factor);
        return Pair.of(multiply, meterValue + "*" + factor);
    }

    public BigDecimal getCacheFactor(String type, Date date) {
        DateTime dateTime = DateUtil.beginOfMonth(date);
        String key = key(type, dateTime);
        String val = redisHelper.get(key);
        if (StringUtils.isNotEmpty(val)) {
            return new BigDecimal(val);
        }
        BigDecimal factor = getFactor(type, dateTime);
        return Objects.isNull(factor) ? BigDecimal.ZERO : setCache(type, dateTime, factor);
    }

    /**
     * 设置缓存
     *
     * @param energyTypeId
     * @param date
     * @param factor
     * @return
     */
    public BigDecimal setCache(String energyTypeId, Date date, BigDecimal factor) {
        redisHelper.set(key(energyTypeId, date), Y666Utils.toStr(factor, 8), expire);
        return factor;
    }

    public String key(String type, Date date) {
        return buildKey(getKeyPrefix(), type, Y666Utils.format(date, "yyyy-MM"));
    }

    public static String buildKey(String... keys){
        return Arrays.stream(keys).collect(Collectors.joining(":"));
    }

    abstract String getKeyPrefix();

    abstract BigDecimal getFactor(String type, DateTime date);

}
